<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/validate.jsp"%>
<%!
	// 添加token
	public Message addToken(DeviceToken deviceToken) {
		Message msg = new Message();
		try {
			Integer hospital = deviceToken.getHospital();
			String token = Tools.getDesc(deviceToken.getToken(), "");
			String version = Tools.getDesc(deviceToken.getVersion(), "");
			Integer uid = deviceToken.getUid();
			if (hospital == null || uid == null || "".equals(token) || "".equals(version)) {
				return new Message(30002, "添加设备号失败");
			}
			List<DeviceToken> dList = QBuilder.db("basedb").select().from("DeviceToken").where(new Where("hospital=", hospital).and("token=", token).and("uid=", uid).and("status=", STATUS_ENABLE)).page(1,1).query().getResultObj();
			if(dList.size() == 0) {
				 DBTools.db("basedb").addItem(deviceToken);
			} else {
				DeviceToken old = dList.get(0);
				deviceToken.setcTime(old.getcTime());
				deviceToken.setId(old.getId());
				DBTools.db("basedb").modItem(deviceToken, deviceToken.getId());
			}
		} catch (Exception e) {
			return new Message(30002, "添加设备号失败");
		}
		return msg;
	}
%>
<%
	// iOS 1  Android 2   wechat   3
	Integer client = input.getInt("client", 1);
	Integer uid = input.getInt("uid", -1);
	String version = input.getDesc("version");
	String token = input.getDesc("device_token");
	DeviceToken deviceToken = new DeviceToken();
	deviceToken.setHospital(hospital);
	deviceToken.setAppType(appType);
	deviceToken.setClient(client);
	deviceToken.setToken(token);
	deviceToken.setUid(uid);
	deviceToken.setVersion(version);
	Tools.encryptPrint(out, addToken(deviceToken));
%>
